package com.liu.qqRobot.wm.handler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.common.utils.QQClientUtils;
import com.liu.mybatisPlus.q.service.QGroupUserOperationQuantityService;
import com.liu.mybatisPlus.wm.entity.base.WmRivenListen;
import com.liu.mybatisPlus.wm.service.WmRivenListenService;
import com.liu.qqRobot.event.entity.dto.EventReportingDTO;
import com.liu.qqRobot.q.enums.OperationTypeEnum;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class WmrlHandler {

    @Resource
    private WmRivenListenService wmRivenListenService;

    @Resource
    private QGroupUserOperationQuantityService qGroupUserOperationQuantityService;

    /**
     * 处理事件
     * @param data 信息内容
     * @param groupId 群号
     * @param userId 发送人QQ号
     */
    public void handler(EventReportingDTO.Message.Data data, Long groupId, Long userId, Boolean isGroup) {

        QueryWrapper<WmRivenListen> wmRivenListenQueryWrapper = new QueryWrapper<>();
        wmRivenListenQueryWrapper.eq("user_id", userId);
        wmRivenListenQueryWrapper.eq("group_id", groupId);
        wmRivenListenQueryWrapper.eq("is_delete", false);
        WmRivenListen wmRivenListen = wmRivenListenService.getOne(wmRivenListenQueryWrapper);
        if (wmRivenListen != null) {
            QQClientUtils.sendMsg("[CQ:at,qq=" + userId + "]\n你在本群已经存在任务，参数：" + wmRivenListen.getParam() + "\n" +
                    "请先删除任务再新建", userId, groupId, isGroup);
            return;
        }
        wmRivenListen = new WmRivenListen();
        wmRivenListen.setUserId(userId);
        wmRivenListen.setGroupId(groupId);
        wmRivenListen.setParam(data.getText());
        wmRivenListen.setIsGroup(isGroup);
        wmRivenListenService.save(wmRivenListen);
        QQClientUtils.sendMsg("[CQ:at,qq=" + userId + "]\n定时监听启动每分钟执行一次，当有符合参数的紫卡将在群里艾特，参数：" + data.getText() + "\n" +
                "（当参数无法使用时，会自动停止任务并通知）", userId, groupId, isGroup);
        qGroupUserOperationQuantityService.statisticsOperationQuantityByType(groupId, userId, OperationTypeEnum.WMRL);
    }

}
